package com.jeady.jTools.imitateChat

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatTextView
import com.jeady.jTools.R


class JTextView : AppCompatTextView{
    val TAG = "JTextView"

    private val density: Float = context.resources.displayMetrics.density

    val lp = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT)

    var ps = 20f
    var pe = 0f

    val myPadding = 15

    constructor(context: Context, direction: Int): super(context){
        if(direction == 1){
            this.ps = 0f
            this.pe = 20f
            lp.gravity = Gravity.END
        }
        setPadding((ps + myPadding).toInt(), myPadding, (pe + myPadding).toInt(), myPadding)

        lp.setMargins(0, 10, 0, 10)
        layoutParams = lp
    }

    constructor(context: Context): super(context){
        ps = paddingStart / density
        pe = paddingEnd / density
        setPadding((ps + myPadding).toInt(), myPadding, (pe + myPadding).toInt(), myPadding)
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet){
        ps = paddingStart / density
        pe = paddingEnd / density
        setPadding((ps + myPadding).toInt(), myPadding, (pe + myPadding).toInt(), myPadding)
    }

    constructor(context: Context, attributeSet: AttributeSet, style: Int): super(context, attributeSet, style) {
        ps = paddingStart / density
        pe = paddingEnd / density
        setPadding((ps + myPadding).toInt(), myPadding, (pe + myPadding).toInt(), myPadding)
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        val paint = Paint()
        val path = Path()

        if(ps == 20f){
            paint.color = resources.getColor(R.color.white,null)
            path.moveTo(ps, 30f)
            path.lineTo(0f, 45f)
            path.lineTo(ps, 60f)
            path.close()
        }
        if(pe == 20f) {
            paint.color = resources.getColor(R.color.lightgreen,null)
            path.moveTo((width - pe).toFloat(), 30f)
            path.lineTo(width.toFloat(), 45f)
            path.lineTo((width - pe).toFloat(), 60f)
            path.close()
        }
        canvas!!.drawRoundRect(
            RectF(ps, 0f, width -pe, height.toFloat()),
            15f, 15f, paint)
        canvas.drawPath(path, paint)
        super.onDraw(canvas)
    }
}